<template>
  <div class="list-table">
    <div class="nospecialModel ">
      <div class="area-header">
        <span class="area-header-title">{{ $t('主要客户') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('年份')" prop="year">
          <DatePicker
            v-model="queryParams.year"
            type="year"
            format="yyyy"
            value-format="yyyy"
            :placeholder="$t('年份')"
          />
        </el-form-item>
        <el-form-item :label="$t('客户名称')" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            :placeholder="$t('客户名称')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['deal:majorclient:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t('新增 - 修改') }}</el-button>
        </el-col>
      </el-row>
    </div>

    <div class="specialModel">
      <!-- 不可删除 -->
      <div class="area-header left">
        <span class="area-header-title">{{ $t('主要客户') }}</span>
      </div>
      <div v-if="sourceType !== 'bussRisk'" class="right">
        <el-button
          v-hasPermi="['deal:majorclient:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </div>
    </div>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealMajorClientList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('客户名称')" align="center" show-overflow-tooltip prop="customerName" />
      <el-table-column :label="$t('销售占比(%)')" align="center" prop="proportionOfSales" width="130">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.proportionOfSales, '') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('订单金额') + $t('金额单位')" align="center" prop="orderAmount" width="130">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.orderAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="sourceType !== 'bussRisk'" :label="$t('备注')" align="center" show-overflow-tooltip prop="remark" />
      <!--<el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:majorclient:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:majorclient:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改主要客户对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealMajorClientForm ref="dealMajorClientForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealMajorClient, delDealMajorClient, exportDealMajorClient } from '@/api/deal/majorclient/dealMajorClient.js'
import dealMajorClientForm from './dealMajorClientForm.vue'
export default {
  components: {
    dealMajorClientForm
  },
  props: {
    sourceType: {
      type: String,
      default: ''
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 主要客户表格数据
          dealMajorClientList: [],
          // 弹出层标题
          title: '主要客户',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              companyId: this.$route.query.companyId,
              year: new Date(),
              customerName: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          }
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询主要客户列表 */
    getList() {
        this.loading = true
        this.queryParams.year = this.formatNowYear()
        listDealMajorClient(this.queryParams).then(response => {
            this.dealMajorClientList = response.rows
            this.total = response.total
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: this.$route.query.companyId,
            year: new Date(),
            customerName: undefined,
            proportionOfSales: undefined,
            orderAmount: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        if (this.queryParams.year === null || this.queryParams.year === undefined || this.queryParams.year === '') {
          this.queryParams.year = new Date()
        }
        this.queryParams.pageSize = 10
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.title = this.$t('主要客户 ({year}年)', { year: this.formatNowYear() })
        this.$nextTick(() => {
            this.$refs.dealMajorClientForm.parentPageYear = this.formatNowYear()
            this.$refs.dealMajorClientForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealMajorClientForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealMajorClient(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportDealMajorClient(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    /** 获取当前年份 */
    formatNowYear() {
      var year = this.queryParams.year
      if (typeof year === 'string' || typeof year === 'number') {
        return year
      } else if (typeof year === 'object') {
        return this.queryParams.year.getFullYear().toString()
      } else {
        return new Date().getFullYear()
      }
    }
  }
}
</script>
